package com.wf.smartcomm.server.serviceImpl;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.wf.smartcomm.server.bean.Complain;
import com.wf.smartcomm.server.bean.Feedback;
import com.wf.smartcomm.server.bean.Praise;
import com.wf.smartcomm.server.bean.Repair;
import com.wf.smartcomm.server.model.Page;
import com.wf.smartcomm.server.service.FeedbackService;
import com.wf.smartcomm.server.util.IdUtils;

/** 
 * @author 38884306@qq.com  
 * @version 创建时间：2015-5-20 下午5:35:48 
 * 
 */
@Service
@Transactional
public class FeedbackServiceImpl implements FeedbackService {

	@Resource
	private SessionFactory factory;
	
	@Override
	public void save(Feedback feedback) {
		feedback.setObjectId(IdUtils.uuid());
		feedback.setCreatedAt(String.valueOf(System.currentTimeMillis()));
		factory.getCurrentSession().persist(feedback);
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public void listByComplain(Complain complain, Page page) {
		Query query = factory.getCurrentSession().createQuery("select count(*) from Feedback where complain = :complain");
		query.setParameter("complain", complain);
		Long counts = (Long) query.uniqueResult();
		page.setTotalCount(counts.intValue());
		query = factory.getCurrentSession().createQuery("from Feedback feedback where feedback.complain = :complain order by feedback.createdAt");
		query.setParameter("complain", complain);
		query.setMaxResults(page.getPageSize());
		query.setFirstResult(page.getBeginIndex());
		page.setDataList(query.list());
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public void listByPraise(Praise praise, Page page) {
		Query query = factory.getCurrentSession().createQuery("select count(*) from Feedback where praise = :praise");
		query.setParameter("praise", praise);
		Long counts = (Long) query.uniqueResult();
		page.setTotalCount(counts.intValue());
		query = factory.getCurrentSession().createQuery("from Feedback feedback where feedback.praise = :praise order by feedback.createdAt");
		query.setParameter("praise", praise);
		query.setMaxResults(page.getPageSize());
		query.setFirstResult(page.getBeginIndex());
		page.setDataList(query.list());
	}

	@Override
	@Transactional(propagation = Propagation.NOT_SUPPORTED)
	public void listByRepair(Repair repair, Page page) {
		Query query = factory.getCurrentSession().createQuery("select count(*) from Feedback where repair = :repair");
		query.setParameter("repair", repair);
		Long counts = (Long) query.uniqueResult();
		page.setTotalCount(counts.intValue());
		query = factory.getCurrentSession().createQuery("from Feedback feedback where feedback.repair = :repair order by feedback.createdAt");
		query.setParameter("repair", repair);
		query.setMaxResults(page.getPageSize());
		query.setFirstResult(page.getBeginIndex());
		page.setDataList(query.list());
	}

}
